//
//  NetworkTools.swift
//  WeChat
//
//  Created by 梁迪 on 15/12/21.
//  Copyright © 2015年 梁迪. All rights reserved.
//

import AFNetworking

//枚举任意类型
enum HTTPMethod: String {
    
    case GET = "GET"
    case POST = "POST"
    
}

let dataErrorDomain = "com.baidu.data.error"

class NetworkTools: AFHTTPSessionManager {
// 单例对象
    static let sharedTools:NetworkTools = {
        let urlString = "https://api.weibo.com/"
        let url = NSURL(string: urlString)
        let tools = NetworkTools(baseURL:url)
    tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
        
    }()
    
    // 网络方法的核心请求
    func requestJSONDict(method:HTTPMethod,urlString:String,parameters:[String:String]?,finished:(dict:[String:AnyObject]?,error:NSError?) ->()){
        
        if method == HTTPMethod.POST{
        
        POST(urlString, parameters: parameters, progress: { (p) -> Void in
            print(p)
            }, success: { (_, request) -> Void in
                
                print(request)
                
                guard let dict = request as? [String:AnyObject] else{
                    
                    let myError = NSError(domain: dataErrorDomain, code: -1000, userInfo: [NSLocalizedDescriptionKey:"数据不合法"])
                    print(myError)
                    finished(dict: nil, error: myError)
                    return
   
                }
                
                
                finished(dict: dict, error: nil)
     
                
            }) { (_, error) -> Void in
                print(error)
                
                finished(dict: nil, error: error)
        }
 
//        GET(urlString, parameters: parameters, progress: { (p) -> Void in
//            
//            print(p)
//            }, success: { (_, request) -> Void in
//                
//            }) { (_, error) -> Void in
//                
//        }
    
            
            
        }else {
            
            GET(urlString, parameters: parameters, progress: { (p) -> Void in
                
                
                }, success: { (_, request) -> Void in
                    
                    guard let dict = request as? [String:AnyObject] else{
                        let myError = NSError(domain: dataErrorDomain, code: -10001, userInfo: [NSLocalizedDescriptionKey:"数据不合法"])
                        
                        finished(dict: nil, error: myError)
                        
                        
                        return
                    }
                    finished(dict: dict, error: nil)
                    
                }, failure: { (_, error) -> Void in
                    
                    
                    finished(dict: nil, error: error)
                    
            })
            
            
            
            
        }
        
            
            
        
        }
        
        
        
    func uploadImage(urlString:String,parameters:[String:String]?,imageData:NSData,finished:(dict:[String:AnyObject]?,error:NSError?) ->()){
        POST(urlString, parameters: parameters, constructingBodyWithBlock: { (formData) -> Void in
            
            formData.appendPartWithFileData(imageData,name:"pic",fileName:"XXOO",mimeType:"image/jpep")
            
            
            }, progress: { (p) -> Void in
                print(p)
            }, success: { (_, request) -> Void in
//                prin?(request)
                if let dict = request as? [String: AnyObject]{
                    
                    finished(dict:dict,error: nil)
                }
                
                
            }) { (_, error) -> Void in
//                print(error)
                finished(dict:nil,error: error)
                
        }
        
        
        
    }
    
    
    
    
    
    
    
        
        
    }
    
    
    
    
    
    
    
    

